//------------------------------------------------------------------------------
//	file sm-test.cpp
//	brief
//		state machine test harness
//
//	$Id: sm-test.cpp 235 2014-01-14 02:54:35Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//
//	This file is part of SM: A State Machine Library.
//
//	SM is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	SM is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with SM.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <popt.h>
#include <conio.h>

#include <stdint.h>									//	Standard int types
#include <stdbool.h>								//	Bool

#include "bi.h"
#include "debug.h"

#include "sm-full.h"
#include "sm-auto.h"

//------------------------------------------------------------------------------
// Local defines
//------------------------------------------------------------------------------
#define OPT_AUTO			0x0001
#define OPT_FULL			0x0002
#define OPT_ABOUT			0x0100
#define OPT_VERBOSE			0x0200

//------------------------------------------------------------------------------
// Local data
//------------------------------------------------------------------------------
static poptContext		optCon;

//------------------------------------------------------------------------------
// Local functions
//------------------------------------------------------------------------------
static void about(void);

//------------------------------------------------------------------------------
// Main function
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	uint32_t	opt=0;
	int			idx=0;
	int			in_char=0;
	bool		tr_run = true;
	bool		verbose = false;

	struct poptOption optionsTable[] = {
		{ "auto",	'u', POPT_ARG_NONE,	  NULL,			 'u', "use auto state machine",				NULL	},
		{ "full",	'f', POPT_ARG_NONE,	  NULL,			 'f', "use full state machine",				NULL	},
		{ "about",	'a', POPT_ARG_NONE,	  NULL,			 'a', "display information about the app",	NULL	},
		{ "verbose",'v', POPT_ARG_NONE,	  NULL,			 'v', "enable verbose mode",				NULL	},
		POPT_AUTOHELP
		POPT_TABLEEND
	};

//------------------------------------------------------------------------------
//  Process arguments
//------------------------------------------------------------------------------
	optCon = poptGetContext(NULL, argc, (const char **)argv, optionsTable, 0);
	poptSetOtherOptionHelp(optCon, "[OPTIONS]*");
	if (argc == 1) {
		about();
	}
	while ((idx = poptGetNextOpt(optCon)) >= 0) {
		switch (idx) {
		case 'u':
			opt |= OPT_AUTO;
			break;
		case 'f':
			opt |= OPT_FULL;
			break;
		case 'a':
			opt |= OPT_ABOUT;
			break;
		case 'v':
			opt |= OPT_VERBOSE;
			break;
		}
	}
	if (idx < -1) {
		fprintf(stderr, "%s: %s\n",
		        poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		        poptStrerror(idx));
		exit(opt);
	}
	if (opt & OPT_ABOUT) {
		bldinfoPrintBldInfo();
		exit(0);
	}
	if (opt & OPT_VERBOSE) {
		verbose = true;
		TRACE_init(true);
	}

//------------------------------------------------------------------------------
//  Get characters from stdin and send them to stdout
//------------------------------------------------------------------------------
	idx = 0;
	while (tr_run == true) {
		in_char = _getch();
		tr_run = (in_char == 'q') ? false : true;
		if (verbose == false)
			printf("%02x ",in_char);
		if (opt & OPT_FULL)	sm_full_process(in_char);
		if (opt & OPT_AUTO)	sm_auto_process(in_char);
	}
	return 0;
}

//------------------------------------------------------------------------------
//	brief
//		Display about
//
//	param
//		none
//
//	return
//		none
//------------------------------------------------------------------------------
static void about(void)
{
	bldinfoPrintBldInfo();
	poptPrintUsage(optCon, stderr, 0);
	exit(1);
}

//
//  End: sm-test.cpp
//
